

const { sqlPromise } = require("../db/sqlPromise");
const timeset = require("../util/timeset")

exports.getGoods = async (req, res) => {
    let sql =   req.query.query? 
    `select goods_id,goods_name,goods_price,goods_number,goods_weight,goods_state,add_time,upd_time,hot_mumber,is_promote 
    from sp_goods where goods_name like "%${req.query.query}%" 
    limit ${(req.query.pagenum - 1) * req.query.pagesize}, ${req.query.pagesize}`
    :  `select goods_id,goods_name,goods_price,goods_number,goods_weight,goods_state,add_time,upd_time,hot_mumber,is_promote
     from sp_goods limit ${(req.query.pagenum - 1) * req.query.pagesize}, ${req.query.pagesize}`

     console.log(req.query.query)
  let results = await sqlPromise(sql);

  if (!results.length) return res.send({ msg: "查询为空" });

  let totalSql = req.query.query? `select COUNT(*) from sp_goods where goods_name like "%${req.query.query}%"`
  : `select COUNT(*) from sp_goods`
    let total = await sqlPromise(totalSql)
console.log((new Date()).getTime())
  res.send({
      total: total[0]["COUNT(*)"],
      pagenum: req.query.pagenum,
    data: results,
    meta: {
      msg: "获取成功",
      status: 200,
    },
  });
};

exports.addGoods = async (req, res) => {
  let sql = "select * from sp_goods where goods_name=?";
  let results = await sqlPromise(sql, [req.body.goods_name]);
  if (results.length > 0) return res.send({ status: 100, msg: "该商品已存在" });

  let time = timeset()
  let cat = (req.body.goods_cat).split(",")
console.log(cat)

 let insertSql = `insert into sp_goods 
  (goods_name,cat_one_id,cat_two_id,cat_three_id,goods_price,goods_number,goods_weight,goods_introduce,add_time,upd_time)
   values ('${req.body.goods_name}','${cat[0]}','${cat[1]}','${cat[2]}','${req.body.goods_price}','${req.body.goods_number}','${req.body.goods_weight}','${req.body.goods_introduce}','${time}','${time}')`;
  
   let insertRes = await sqlPromise(insertSql);
  console.log(insertRes);

  if (insertRes.affectedRows != 1)
    return res.send({ msg: "添加失败 未知错误" });

  results = await sqlPromise(sql, [req.body.goods_name]);

  res.send({
    data: {
      goods_name: results[0].goods_name,
      goods_cat: `${results[0].cat_one_id},${results[0].cat_two_id},${results[0].cat_three_id}`,
      goods_price: results[0].goods_price,
      goods_number: results[0].goods_number,
      goods_weight: results[0].goods_weight,
      goods_introduce: results[0].goods_introduce,
    },
    meta: {
      msg: "创建成功",
      status: 201,
    },
  }); 
};

exports.delGood = async (req, res) => {
    let sql = "DELETE FROM sp_goods WHERE goods_id=?";
    let results = await sqlPromise(sql, [req.params.id]);
    if (results.affectedRows != 1)
      return res.send({
        msg: "删除失败",
        status: 100,
      });
  
    res.send({
      data: null,
      meta: {
        msg: "删除成功",
        status: 200,
      },
    });
};

exports.getGood = async (req, res) => {
    let sql = "select * from sp_goods where goods_id = ?";
    let results = await sqlPromise(sql, [req.params.id]);
    if (!results.length) return res.send({ msg: "查询为空" });


  res.send({
    data: results,
    meta: {
      msg: "获取成功",
      status: 200,
    },
  });
};

exports.editGood = async (req, res) => {
    let sql1 = `select * from sp_goods where goods_id='${req.params.id}'`;
    let result1 = await sqlPromise(sql1);
    if (!result1.length) return res.send({ status: 100, msg: "此id不存在" });

// goods_name,cat_one_id,cat_two_id,cat_three_id,goods_price,goods_number,goods_weight,goods_introduce,add_time,upd_time
    let updateSql = `UPDATE sp_goods SET goods_name='${req.query.goods_name}',goods_price='${req.query.goods_price}',goods_number='${req.query.goods_number}',goods_weight='${req.query.goods_weight}' WHERE goods_id='${req.params.id}'`;
    let upResult = await sqlPromise(updateSql);
    if (upResult.affectedRows != 1)
      return res.send({ status: 100, msg: "角色编辑失败" });
  
    let endResult = await sqlPromise(sql1);

    res.send({
      data: endResult,
      meta: {
        msg: "角色编辑成功",
        status: 200,
      },
    });
};

